package com.mall.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.mall.dto.OrderCancelDTO;
import com.mall.dto.OrderConfirmDTO;
import com.mall.dto.OrderRejectionDTO;
import com.mall.dto.OrdersPageQueryDTO;
import com.mall.po.OrderDetail;
import com.mall.po.Orders;
import com.mall.result.PageResult;
import com.mall.result.Result;
import com.mall.service.OrdersService;
import com.mall.vo.OrderCountVO;
import com.mall.vo.OrdersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单信息 前端控制器
 * </p>
 *
 * @author itlxc
 * @since 2024-04-28
 */
@RestController
@RequestMapping("/admin/order")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "订单管理相关接口")
public class OrdersController {

    private final OrdersService ordersService;

    /**
     * 订单搜索
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> ordersPageQuery(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单搜索：{}", ordersPageQueryDTO);
        PageResult pageResult = ordersService.ordersPageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrdersVO> getOrderById(@PathVariable Long id) {
        log.info("查询订单详情：{}", id);
        Orders orders = ordersService.getById(id);
        log.info("订单信息：{}", orders);
        List<OrderDetail> orderDetailList = Db.lambdaQuery(OrderDetail.class)
                                            .eq(OrderDetail::getOrderId, id)
                                            .list();
        log.info("订单详情：{}", orderDetailList);
        OrdersVO ordersVO = BeanUtil.copyProperties(orders, OrdersVO.class);
        ordersVO.setOrderDetailList(orderDetailList);
        return Result.success(ordersVO);
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderCountVO> getStatistics() {
        log.info("各个状态的订单数量统计");
        return Result.success(ordersService.getStatistics());
    }

    /**
     * 商家接单
     * @param orderConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("商家接单")
    public Result confirmOrders(@RequestBody OrderConfirmDTO orderConfirmDTO) {
        log.info("商家接单：{}", orderConfirmDTO);
        ordersService.confirmOrders(orderConfirmDTO);
        return Result.success();
    }

    /**
     * 商家拒单
     * @param orderRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("商家拒单")
    public Result rejectionOrders(@RequestBody OrderRejectionDTO orderRejectionDTO) {
        log.info("商家拒单：{}", orderRejectionDTO);
        ordersService.rejectionOrders(orderRejectionDTO);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result deliveryOrders(@PathVariable Long id) {
        log.info("派送订单：{}", id);
        ordersService.deliveryOrders(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result completeOrders(@PathVariable Long id) {
        log.info("完成订单：{}", id);
        ordersService.completeOrders(id);
        return Result.success();
    }

    /**
     * 商家取消订单
     * @param orderCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("商家取消订单")
    public Result cancelOrders(@RequestBody OrderCancelDTO orderCancelDTO) {
        log.info("商家取消订单：{}", orderCancelDTO);
        ordersService.cancelOrders(orderCancelDTO);
        return Result.success();
    }
}
